package gang.org.springframework.framework.aop.config;

import gang.org.springframework.framework.aop.autoproxy.GangInfrastructureAdvisorAutoProxyCreator;
import gang.org.springframework.framework.bean.GangBeanDefinition;
import gang.org.springframework.framework.bean.GangRootBeanDefinition;
import gang.org.springframework.framework.support.GangBeanDefinitionRegistry;

/**
 * 类说明：gang.chen springboot深度实践
 *
 * @author gang.chen
 * @since 2022/5/20
 */
public abstract class GangAopConfigUtils {

    public static final String auto_proxy_creator_bean_name = "org.springframework.aop.config.internalAutoProxyCreator";

    public static GangBeanDefinition registerAutoProxyCreatorIfNecessary(GangBeanDefinitionRegistry registry){
        return registerAutoProxyCreatorIfNecessary(registry, null);
    }

    public static  GangBeanDefinition registerAutoProxyCreatorIfNecessary(GangBeanDefinitionRegistry registry, Object source){
        return registerOrEscalateApcAsRequired(GangInfrastructureAdvisorAutoProxyCreator.class, registry, source);
    }

    /**
     * Escalate 升级
     * */
    public static GangBeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, GangBeanDefinitionRegistry registry, Object source){
        if (registry.containsBeanDefinition(auto_proxy_creator_bean_name)) {
            GangBeanDefinition apcDefinition = registry.getBeanDefinition(auto_proxy_creator_bean_name);
            //TODO

            return null;
        }
        GangRootBeanDefinition beanDefinition = new GangRootBeanDefinition(cls);
        beanDefinition.setSource(source);
        beanDefinition.setRole(GangBeanDefinition.ROLE_INFRASTRUCTURE);
        registry.registerBeanDefinition(auto_proxy_creator_bean_name, beanDefinition);
        return beanDefinition;
    }

    public static void forceAutoProxyCreatorToUseClassProxying(GangBeanDefinitionRegistry registry){
        //TODO
    }
}
